package com.atguigu.demojuc.chap04;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author njg
 * @version 1.0
 * @date 2025/6/17 19:00
 */
public class NoSafeDemo {
    public static void main(String[] args) {
//        extracted();
//        extracted1();
        extracted2();
    }

    private static void extracted2() {
//        HashMap<String, String> map = new HashMap<>();
        ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
        for (int i = 0; i < 100; i++) {
            String value = String.valueOf(i);
            new Thread(() -> {
                map.put(UUID.randomUUID().toString().substring(0,8),value);
                System.out.println(map);
            }, "i").start();
        }
    }

    private static void extracted1() {
//        HashSet<String> set = new HashSet<>();
        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(set);
            }, "i").start();
        }
    }

    private static void extracted() {
//        ArrayList<String> list = new ArrayList<>();
//        Vector<String> list = new Vector<>();
//        Collection<String> list = Collections.synchronizedCollection(new ArrayList<String>());
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(list);
            }, "i").start();
        }
    }
}
